package online.yysh.charging;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;

import online.yysh.charging.base.BaseActivity;
import online.yysh.charging.bean.request.GetAllStationsRequest;
import online.yysh.charging.bean.response.GetStationsResponse;
import online.yysh.charging.bean.response.Station;
import online.yysh.charging.utils.HttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class VagueQueryActivity extends BaseActivity {

    private TextView tv_cancel,tv_title;
    private EditText et_search;
    private RecyclerView rv_station;
    private List<Station> stations = new ArrayList<>();
    private MyAdapter adapter;
    private String myLon,myLat;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 200:
                    adapter.notifyDataSetChanged();
                    break;
                case 201:

                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_vague_query;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText(R.string.search_station);
        rv_station = findViewById(R.id.rv_station);
        et_search = findViewById(R.id.et_search);
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                getStations(s.toString());
            }
        });
    }

    private void getStations(String s) {
        GetAllStationsRequest request = new GetAllStationsRequest(myLon,myLat,s);
        HttpUtil.getInstance().getStations(request, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                showToast("查询失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                logE("getStations:"+result);
                GetStationsResponse getStationsResponse = new Gson().fromJson(result,GetStationsResponse.class);
                if ("ok".equals(getStationsResponse.getStatus())){
                    List<Station> stationList = getStationsResponse.getData();
                    stations = stationList;
                    handler.sendEmptyMessage(200);
                }else{
                    showToast("查询失败");
                }
            }
        });
    }

    @Override
    protected void initData() {
        String[] data = getIntent().getStringExtra("data").split("_");
        myLon = data[0];
        myLat = data[1];
        rv_station.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyAdapter();
        rv_station.setAdapter(adapter);
        getStations("");
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        @NonNull
        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_station, parent, false);
            return new MyAdapter.MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
            final Station station = stations.get(position);
            holder.tv_name.setText("电站名:"+station.getName());
            holder.tv_price.setText("电价:"+station.getDQPrice()+"元/度，服务费:"+station.getService()+"元/度");
            holder.tv_distance.setText("距离："+station.getDistance()+"km");
            holder.tv_address.setText("地址："+station.getAddress());
            holder.btn.setText(station.getKXPortCount()+"/"+station.getPortCount());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Station bean = new Station();
                    bean.setID(station.getID());
                    bean.setName(station.getName());
                    Intent intent = new Intent(VagueQueryActivity.this,SubscribeActivity.class);
                    intent.putExtra("chargingBean",bean);
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return stations.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tv_name, tv_distance, tv_price, tv_address;
            Button btn;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_distance = itemView.findViewById(R.id.tv_distance);
                tv_address = itemView.findViewById(R.id.tv_address);
                tv_price = itemView.findViewById(R.id.tv_price);
                btn = itemView.findViewById(R.id.btn);
            }
        }
    }
}